package com.ruangong.GitClient.GiteeAPI;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import okhttp3.Response;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class CodeBackups {
    String responseBodyJson = "Access token: {\"access_token\":\"c7024531e3dac960466431dace160cd7\",\"token_type\":\"bearer\",\"expires_in\":86400,\"refresh_token\":\"740107e2c89b2c58f7f4ceaf33edb64d0570a4c2020593b6564e6695013a304e\",\"scope\":\"user_info projects pull_requests issues notes keys hook groups gists enterprises emails\",\"created_at\":1714714308}";
    /**
     * Access token: {
     * "access_token":"c7024531e3dac960466431dace160cd7",
     * "token_type":"bearer","expires_in":86400,
     * "refresh_token":"740107e2c89b2c58f7f4ceaf33edb64d0570a4c2020593b6564e6695013a304e",
     * "scope":"user_info projects pull_requests issues notes keys hook groups gists enterprises emails",
     * "created_at":1714714308}
     */
//    try (
//    Response response = client.newCall(request).execute()) {
//        if (response.isSuccessful()) {
//            String responseBody = response.body().string();
//            System.out.println(responseBody);
//            Gson gson = new Gson();
//            Type listType = new TypeToken<List<GiteeFile>>(){}.getType();
//            GiteeRepository giteeRepository=GiteeRepository.fromJson(responseBody);
////                System.out.println(giteeRepository);
//            List<GiteeFile> giteeFiles=giteeRepository.getFiles();
//            for (GiteeFile giteeFile : giteeFiles) {
//                System.out.println("Type: " + giteeFile.getType() + ", Name: " + giteeFile.getName() + ", Path: " + giteeFile.getPath());
//
//                if ("dir".equals(giteeFile.getType())) {
////                        exploreRepository(owner, repo, giteeFile.getPath()); // 递归调用以遍历目录
//                    giteeFile.showDirAllFiles();
//                }
//                else if ("file".equals(giteeFile.getType())) {
//                    giteeFile.showFile();
//                }
//            }
//
//
//        } else {
//            System.err.println("Failed to fetch repository contents: " + response.message());
//        }
//    } catch (
//    IOException e) {
//        e.printStackTrace();
//    }

    /*
    private static void exploreRepository(String owner, String repo, String path) {
        Gson gson = new Gson();
        String url = BASE_URL + owner + "/" + repo + "/contents/" + path + "?access_token=" + ACCESS_TOKEN;
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
//                System.out.println(responseBody);
                Type listType = new TypeToken<List<GiteeFile>>() {
                }.getType();
                List<GiteeFile> contents = gson.fromJson(responseBody, listType);

                for (GiteeFile item : contents) {
                    System.out.println("Type: " + item.getType() + ", Name: " + item.getName() + ", Path: " + item.getPath());

                    if ("dir".equals(item.getType())) {
                        exploreRepository(owner, repo, item.getPath()); // 递归调用以遍历目录
                    } else if ("file".equals(item.getType())) {
                        item.showFile();
                    }
                }
            } else {
                System.err.println("Failed to fetch repository contents: " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     */
}

